"use strict";
var chai = require('chai');
var co = require('co');
var moment = require('moment');
var expect = chai.expect;
var q = require('q');
var models = require('../models/index.js');
var DistHelper = require('../service/DistHelper');


describe('moment测试', function () {
    it('#moment的时间,timezone设置', function () {
        var t = moment(new Date("2015-12-01 21:00:38.976 +00:00")).format("YYYY-MM-DD HH:mm:ss");
        console.log(t);
    });
});

describe('测距仪控制测试', function () {
    this.timeout(1000 * 36000);
    it.skip('#能够正确的从数据库中获取测量得到的测距仪数据', function () {
        return co(function* () {
            // var t1 = new Date();
            // var t2 = new Date().setHours(t1.getHours() - 1);
            // .format('YYYY-MM-DD HH:mm:ss')
            var deviceList = yield models.DistDeviceResult.findAll({
                where: {
                    configId: 3,
                    date: {
                        $and: {
                            $lte: moment().toDate(),
                            $gte: moment().add(-1, 'h').toDate(),
                        }
                    }
                }
            });
            console.log('\t一共查找到%s个数据', deviceList.length);
            deviceList.forEach(t=> {
                console.log(JSON.stringify(t));
            });
        });
    });

    it('#从数据库里获取正确的测距仪', function () {
        return co(function* () {
            var device = yield models.DistDeviceConfig.findOne({
                where: {
                    prjId: 2,
                    deviceId: 128
                }
            });
            console.log('\t', 'device的id是:', device.id);
            expect(device).not.to.be.equal(null);
        });
    });

    it('#能够正确的开始测量', function (done) {
        co(function* () {
            var device = yield models.DistDeviceConfig.findOne({
                where: {
                    prjId: 2,
                    deviceId: 128
                }
            });
            var helper = new DistHelper({
                device: device,
                prjId: 2
            });
            helper.on('distsurveystart', function () {
                console.log('\t测距仪开始测量了');
            });
            helper.startSurvey();
            setTimeout(function () {
                helper.stopSurvey();
            }, 1000 * 3600);
            helper.on('distsurveystop', function () {
                console.log('\t测距仪停止测量了');
                done();
            });
        });
    })
});